<template>
    <div class="shopping">
      <top-header :isLeft="true" title="奈雪购物车"></top-header>
      <div class="no-shopping" v-if="shopCar.length<=0">
        <img src="../assets/imgs/购物车.jpg" alt="">
          <div class="no-shopping-tips">
            <b>购物车还是空的</b>
          </div>
          <p>赶紧买点宝贝慰劳下自己吧</p>
          <a-button type="primary"
          shape="round"
          ghost
          size="large"
          @click="sort()"
          >去逛逛</a-button>
      </div>
      <div class="container" v-else>

        <!-- 单条数据 -->
        <div class="item" v-for="(item,index) in shopCar" :key="index">
          <div class="logo">
            <img :src="item.logo_img" alt="">
            <span>{{item.logo_name}}</span>
          </div>
          <div class="details">
            <img :src="item.details_img" alt="">
            <div class="details_list">
            <span>{{item.details_top}}</span>
            <div class="list_more">
             <span>{{item.details_center}}</span>
             <span>{{item.details_bottom}}</span>
            </div>
            <div class="list_price">
              ￥{{item.price}}
            </div>
            </div>
          </div>
          <div class="num">
           <span>购买数量</span>
           <div>
             <button class="button-first" @click=btn(false,index)>-</button>
             <input type="text" :value="item.num">
             <button class="button-second" @click=btn(true,index)>+</button>
           </div>
          </div> 
        </div>                     
      </div>       
      <footer>
        <div class="footer_left">
          实际付款：
          <span>￥{{num_price}}</span>
        </div>
        <div class="footer_right">
          <a-button class="footer-button" type="primary" shape="round">结算</a-button>
        </div>
      </footer>
    </div>
  
</template>

<script>
import MyCart from "../components/MyCart.vue"
import TopHeader from "../components/TopHeader.vue";
import BScroll from 'better-scroll'
export default {
  components:{MyCart,TopHeader},
  data() {
    return {
      num_price:0,
      shopCar:[
        {
          id:"001",
          logo_img:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.ddmeishi.com%2Fuploads%2Fallimg%2F200512%2F14-200512141327.jpg&refer=http%3A%2F%2Fwww.ddmeishi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623308392&t=bac32964f79a53a37fcf063176587db3",
          logo_name:"奈雪の茶",
          details_img:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fww3.sinaimg.cn%2Fmw690%2F006V3HA6ly8gqehgl7w8ij30u00u0x17.jpg&refer=http%3A%2F%2Fwww.sina.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623415187&t=dc7ffffdfb7df7507eb474c76d0d88d4",
          details_top:"霸气荔枝",
          details_center:"【650ml】",
          details_bottom:"妃子笑荔枝搭配茉莉初雪茶",
          price:"30",
          num:1
        },
        {
          id:"002",
          logo_img:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.ddmeishi.com%2Fuploads%2Fallimg%2F200512%2F14-200512141327.jpg&refer=http%3A%2F%2Fwww.ddmeishi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623308392&t=bac32964f79a53a37fcf063176587db3",
          logo_name:"奈雪の茶",
          details_img:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fq_70%2Cc_zoom%2Cw_640%2Fimages%2F20190508%2F6636e6becec64a0c99858992d0ce0cdc.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623415821&t=6d37a73cc6bf8f30cc7b078256d8978f",
          details_top:"霸气杨梅",
          details_center:"【650ml】",
          details_bottom:"精选成熟杨梅搭配莱莉初雪",
          price:"30",
          num:1
        },
        {
          id:"003",
          logo_img:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.ddmeishi.com%2Fuploads%2Fallimg%2F200512%2F14-200512141327.jpg&refer=http%3A%2F%2Fwww.ddmeishi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623308392&t=bac32964f79a53a37fcf063176587db3",
          logo_name:"奈雪の茶",
          details_img:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20190603%2F98e4213ed44e487ea2999289b35aa4ad.JPG&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623415922&t=1eef1956986a4e3906cb6dbd74ea2e49",
          details_top:"霸气芝士草莓",
          details_center:"【冷饮650ml 热饮500ml】",
          details_bottom:"新鲜草莓搭配莱莉初雪茶底和芝士奶盖",
          price:"32",
          num:1
        },
        {
          id:"004",
          logo_img:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.ddmeishi.com%2Fuploads%2Fallimg%2F200512%2F14-200512141327.jpg&refer=http%3A%2F%2Fwww.ddmeishi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623308392&t=bac32964f79a53a37fcf063176587db3",
          logo_name:"奈雪の茶",
          details_img:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20181123%2F4ce188577b844c93adec7565ef1bfc43.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623416006&t=9cc2afda63cb6c051eccc8ccad1aa85b",
          details_top:"霸气芝士芒果",
          details_center:"【 650ml】",
          details_bottom:"芒果搭配茉莉初雪茶底与香滑芝士奶盖",
          price:"30",
          num:1
        }
      ]
    }
  },
  methods: {
    sort(){
      this.$router.push({name:"Sort"})
    },
    Totalprice(){
      let price=0;
      this.shopCar.forEach((item,index)=>{
        price+=item.num*item.price
      })
      this.num_price=price.toFixed(2)//保留两位小数
    },
    btn(bool,index){
      //点哪条数据就把那条数据赋值给shopIndex
      let shopIndex=this.shopCar[index]
      if(bool){
        //如果点击的是加号就把num值累加
        shopIndex.num++
      }else{
        if(shopIndex.num<=1){
          this.shopCar.splice(index,1)
        }
        shopIndex.num--
      }
      //重新求一次额
      this.Totalprice()
    },
         
  },
  mounted() {
    this.Totalprice()
  },
}

</script>
<style lang="scss" scoped>
  .shopping{
    position: relative;
    width: 100%;
    height: 85vh;
    overflow: auto;
    .no-shopping{
      // height: 30%;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      img{
        width: 80%;
      }
      .no-shopping-tips {
      display: flex;
      justify-content: center;
      font-size: 1.5rem;
      margin: $common-margin;
      }
      p{
        font-size: 20px;
      }
    }
    
    .container{
      height: 100%; 
      background: $app-bg-color;     
      margin: 10px;
      // border-radius: $common-border-radius;
      .item{
        background: $module-color;
        padding:5px 10px;     
        border-radius: $common-border-radius;
        margin-bottom: 10px;
      }
      .logo{
        height: 40px;
        // 左右浮动布局
        display: flex;
        // 以交叉轴的中点对齐
        align-items: center;
      }
      .logo img{
        width: 25px;
        height: 25px;
        margin-left: 5px;
        border-radius: $common-border-radius;
      }
      .logo span{
        margin-left: 5px;
        font-size: 14px;
      }
      .details{
        background: $app-bg-color;
        padding: 15px;
        font-size: 15px;
        display: flex;
        img{
          width: 110px;
          height: 100px;
          border-radius: $common-border-radius;
        }
      }
      .details_list{
        margin-left: 15px;
        color: $font-color;
      }
      .list_more span{
        color: $small-font-color;
      }
      .details_list span{
          display: block;
          // margin-top: 3px;
      }
      .details_list>:first-child{
        font-size: 20px;
      }
      .list_price{
        color: $font-color;
        font-size: 20px;
      }
      .num{
        color: $small-font-color;
        padding: 10px;
        display: flex;
        justify-content: space-between;
        font-size: 15px;
        span{
          padding: 5px;
        }
        button{
          width: 37px;
          height: 29px;
          background: #f5f5f5;
          // 清除自带边框
          border: none;
          
        }
        .button-first{
          border-top-left-radius:6px;
          border-bottom-left-radius:6px;
        }
        .button-second{
          border-top-right-radius:6px;
          border-bottom-right-radius:6px;
        }
        input{
          width: 40px;
          height: 29px;
          border: none;
          text-align: center;
          background: #eeeeee;
          margin:auto 3px ;
        }
      }
    }

    footer{
      width: 100%;
      height: $footer-height;
      position: fixed;
      bottom: 53px;
      left: 0;
      background: white;
      border-top: 1px solid #ccc;
      display: flex;
      border-radius: 50px;
      .footer_left{
        flex: 1;
        text-align: center;
        line-height: 50px;
        font-size: 18px;
        padding-right: 10px;
        span{
          color: $starbucks-color;
        }
      }
      .footer_right{
        // background: $starbucks-color;
        color: white;
        width: 140px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        padding: 5px;
        .footer-button{
          font-size: 20px;
          width: 100px;
          height: 35px;
        }
      }
    }
    
  }
</style>